from plotly.subplots import make_subplots
import plotly.graph_objs as go
import plotly.offline as py
pyplt = py.offline.plot
import plotly.express as px

import pandas as pd
pd.options.plotting.backend = 'plotly'

class PlotlyMgr:
    def __init__(self, row=2, col=2):
        self.fig = make_subplots(
            rows=row, cols=col,
            # shared_xaxes=True,
            vertical_spacing=0.2,
            specs=[[{}, {"type": "table"}],
                   [{}, {}]]
        )


    def add_lines(self, df, row=1, col=1):
        for c in df.columns:
            trace = go.Scatter(
                x=df.index,
                y=df[c].values,
                mode='lines',
                text=df[c].values,
                name='markers',
                #xaxis=dict(tickangle=45),
                #title = "equity"
            )

            self.fig.add_trace(trace, row, col)

    def add_bar(self, df, row, col):

        for c in df.columns:
            trace = go.Bar(
                x=df.index,
                y=df[c].values,

            )
            self.fig.add_trace(trace, row, col)


    def add_heatmap(self, df, row, col):
        trace = go.Heatmap(
            #showlegend=True,
            #name='数据',

            x=df.index,
            y= df.index,
            z=df.values,
            #z=df['z'],
            #type='heatmap',
        )
        self.fig.add_trace(trace, row, col)

    def add_table(self, df, row=1, col=2):
        df['名称'] = df.index
        print(df.T.values)
        trace = go.Table(
            header=dict(
                values=list(df.columns),
                font=dict(size=10),
                align="left"
            ),

            cells=dict(
                values=df.T.values,
                align="left")
        )
        self.fig.add_trace(trace, row, col)

    def show(self, filename):
        self.fig.update_layout(
            height=800,
            #showlegend=True,
            title_text="策略回测结果分析",
            #xaxis=dict(title='Year'),
            #yaxis=dict(title='equity'),
        )
        self.fig.write_html(filename)